<template>
	<view class="wrap">
		
		<view class="j-table" style="margin-bottom: 200rpx;">
			<view class="f-r-b th p32" style="color: #000;border-width: 2rpx;border-color: #ccc;">
		
				<view class="tr tr1">品牌</view>
				<view class="tr tr2">涨跌幅</view>
				<view class="tr tr3">价格</view>
				<view class="tr tr4" @click="changeAll" v-if="showRadio">{{selectAll?'取消全选':'全选'}}</view>
				<view class="tr tr4" v-else></view>
			</view>
			
			<checkbox-group v-model="checkedArr" @change="radioChange">
				<label class="f-r-b th p32" v-for="(item,index) in tableList" :key="index" style="border-top: none;">
					
					<view class="tr tr1">
						<text>{{item.productName}}</text>
					</view>
					<view class="tr tr2" >
						<view class="dzf f-r-c" :style="{'color':item.biliType==1?'red':item.biliType==2?'green':themeColor}">
							<view class="t">{{item.biliPrice}}</view>
							<uni-icons class="icon" v-if="item.biliType==1" type="arrow-up" size="15" color="red"></uni-icons>
							<uni-icons class="icon" v-else-if="item.biliType==2" type="arrow-down" size="15" color="green"></uni-icons>
							<view class="icon" v-else > - </view>
						</view>
						
					</view>
					<view class="tr tr3" :style="{'color':themeColor}">
						<text>¥{{item.productPrice}}</text>
					</view>
					<view class="tr tr4" v-if="showRadio">
						<checkbox v-model="item.id"  :checked="checkedArr.indexOf(item.id)>-1" :color="themeColor" style="transform:scale(0.7)"/>
					</view>
					<view class="tr tr4" v-else>
						<view class="card-btn f-r-c " :style="{'background':themeColor,}" @click="qxguanzhu(item,index)">
							取消关注
						</view>
						
					</view>
				</label>
			</checkbox-group>
		
		</view>
		<!-- 如果是暂无数据 -->
	    <view class="f-r-c p32 foot-buttom">
			<view class="submit bggradient f-r-c" @click="submit" v-if="showRadio" :style="{'background':checkedArr.length?'':'#eee'}">确定</view>
			<view class="submit bggradient f-r-c" @click="submit2" v-else>导入计算器</view>
		</view>
		
	</view>
</template>

<script>
	export default {
		data() {
			return {
				showBtntype:null,
				showRadio:true,
				checkedArr:[],
				tableList: [],
				checkedTable:[],



			}
		},
		onLoad(option) {
			if(option.showBtntype==2){
				this.showBtntype=2;//来自我的点击进来
				this.showRadio=false;
			}else{
				this.showBtntype=1;//从计算器点击哪里
				this.showRadio=true;
			}
		},
		onShow() {
			this.getList();
		},
		computed: {
			themeColor() {
				return this.$themeColor;
			},
			//全选
			ids() {
			      return this.tableList.map(item => item.id);
			},
			selectAll(){
				return this.tableList.length==this.checkedArr.length&&this.tableList.length>0;
			},
		},

		methods: {
			getList(){
				let opts={
				    url:"/authMarket/getFocusProductList",
				    method: 'get'
				};
				this.$http(opts,{}).then(res=>{
					console.log("我的关注",res.data);
					if(res.code==200){
						this.tableList=res.data;
					}
				})
			},
			//全选
			changeAll(){
				if(this.selectAll){
					this.checkedArr=[];
				}else{
					this.checkedArr=this.ids;
				}				
				
			},
			radioChange(e){
				// console.log(e.detail.value); //字符串数组
				let arr=e.detail.value;
				if(arr.length>0){
					this.checkedArr=arr.map(item => Number(item));
				}else{
					this.checkedArr=[];
				}
				
				
			},
			//保存
			saveData(back){
				let opts={
				    url:'/authMarket/importDetails',
				    method: 'post'
				};
				let param={
					ids:this.checkedArr,
				}
				this.$http(opts,param).then(res=>{
					if(res.code==200){
						uni.showToast({
							title:'导入成功',
							icon:'none',
						})
						if(back){
							setTimeout(function(){
								uni.navigateBack();
							},1400)
						}
						
						
					}else{
						uni.showToast({
							title:'导入失败',
							icon:'none',
						})
					}
				})
				
			},
			submit(){
				if(this.checkedArr.length==0){
					uni.showToast({
						title:'请至少选择一个产品',
						icon:'none',
					})
					return false;
				}
				let that=this;
				uni.showModal({
					title:'导入关注商品',
					content:'已导入的商品会被替换，是否确认导入?',
					success: function (res) {
						if (res.confirm) {
							if(that.showBtntype==1){
								that.saveData(true);
							}else if(that.showBtntype==2){
								that.saveData(false);
								uni.navigateTo({
									url:'/pages/index/calculator'
								})
							}
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
						
					
				})
				
			},
			
			
			
			//取消关注
			qxguanzhu(item,index){
				let opts={
				    url:`/authMarket/addOrRemoveFacus/${item.id}`,
				    method: 'get'
				};
				this.$http(opts,{}).then(res=>{
					console.log("取消关注",res.data);
					if(res.code==200){
						this.tableList.splice(index,1);
						uni.showToast({
							title:'操作成功',
							icon:'none',
						})
					}
				})
				
				
			},
			
			//导入计算器
			submit2(){
				this.showRadio=true;
			},


		},

	}
</script>
<style>
	page{
		background: #f8f8f8;
	}
</style>
<style scoped lang="less">
	.wrap {
		.date {
			color: #999;
			margin-bottom: 24rpx;
		}

		.j-table {
			background: #fff;
			overflow: hidden;

			.th {
				
				height: 100rpx;
				flex-wrap: nowrap;
				border-bottom: 1rpx #eee solid;
				border-top: 1rpx #eee solid;

				.tr {
					height: 100%;
					flex: 1;
					display: flex;
					flex-direction: row;
					align-items: center;
					justify-content: center;
					font-size: 28rpx;
					
					// background: #00000010;
					// border-right: 1rpx #f00 solid;
					text {
						height: 40rpx;
						line-height: 40rpx;
						width: 100%;
						overflow: hidden;
						text-align: center;
						font-size: 26rpx;
					}

				}

				.tr1 {
					justify-content: flex-start;
					text-align: left;
					min-width: 300rpx;
					max-width: 300rpx;
					
					// background: #ccc;

					text {
						text-align: left;
					}
				}
				.tr2{
					.dzf{
						// border: 1rpx #000 solid;
						flex: 1;
						.t{
							// flex: 1;
							// background: #999;
							margin: 0 10rpx;
							// text-align: center;
						}
						.icon{
							// flex: 1;
							// width: 30rpx;
							// height: 40rpx;
							// line-height: 40rpx;
							// text-align: left;
							// background: #ccc;
						}
					}
				}

				.tr4 {
					// background: #ccc;
					min-width: 120rpx;
					max-width: 120rpx;
					.card-btn{
						width: 120rpx;
						height: 50rpx;
						border-radius: 30rpx;
						text-align: center;
					    line-height: 50rpx;
						color: #fff;
						font-size: 23rpx;
						
					}
					
				}
			}

		}

        .foot-buttom{
			width: 100%;
			box-sizing: border-box;
			position: fixed;
			bottom: 50rpx;
			left: 0;
			.submit {
				flex:1;
				border-radius: 50rpx;
				height: 80rpx;
				color: #fff;
			}
		}
		
	}
</style>